package com.example.springtaskschedule.config;

import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;

import java.util.Map;

/**
 * MdcTaskDecorator
 *
 * @author zhouxianfeng
 * @date 2020-12-7
 */
public class MdcTaskDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        Map<String, String> contextMap = MDC.getCopyOfContextMap();

        Runnable runnable1 = new Runnable() {
            @Override
            public void run() {
                if (contextMap != null) {
                    MDC.setContextMap(contextMap);
                }
                runnable.run();
            }
        };
        return runnable1;
    }
}